package com.yhd.service.impl;

import com.github.pagehelper.Page;
import com.yhd.entity.TSystemDictionary;
import com.yhd.entity.dto.SystemDictionaryDto;
import com.yhd.entity.dto.UserDto;
import com.yhd.entity.query.SystemDictionaryVO;
import com.yhd.mapper.CourseMapper;
import com.yhd.mapper.SysDictMapper;
import com.yhd.model.PageInfo;
import com.yhd.model.ResultPage;
import com.yhd.service.BaseService;
import com.yhd.service.SysDictService;
import com.yhd.utils.Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class SysDictServiceImpl extends BaseService<SysDictMapper, TSystemDictionary> implements SysDictService {
    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 新增字典
     * @param systemDictionaryVO
     * @param userInfo
     * @return
     */
    @Override
    public SystemDictionaryDto addSysDict(SystemDictionaryVO systemDictionaryVO, UserDto userInfo) {
        if (null != systemDictionaryVO){
            TSystemDictionary tSystemDictionary = new TSystemDictionary();
            BeanUtils.copyProperties(systemDictionaryVO,tSystemDictionary);
            if (null != systemDictionaryVO.getGroupId() && (systemDictionaryVO.getGroupId().equals("300") || systemDictionaryVO.getGroupId().equals("400"))){
                TSystemDictionary newTSystemDictionary = new TSystemDictionary();
                newTSystemDictionary.setGroupId(systemDictionaryVO.getGroupId());
                TSystemDictionary old = sysDictMapper.selectOne(newTSystemDictionary);
                if (null != old){
                    return null;
                }
            }
            tSystemDictionary.setId(Utils.getId());
            //组id100课程分类 200活动分类
            if (null != systemDictionaryVO.getGroupId() && systemDictionaryVO.getGroupId().equals("100")){
                tSystemDictionary.setGroupName("课程分类");
            }else if (null != systemDictionaryVO.getGroupId() && systemDictionaryVO.getGroupId().equals("200")){
                tSystemDictionary.setGroupName("活动分类");
            }else if (null != systemDictionaryVO.getGroupId() && systemDictionaryVO.getGroupId().equals("500")){
                tSystemDictionary.setGroupName("师资分类");
            }
            //状态 1:生效 0:未生效
//            tSystemDictionary.setStatus(1);
            tSystemDictionary.setCreateDate(new Date());
            tSystemDictionary.setCreateUserId(userInfo.getId());
            tSystemDictionary.setUpdateDate(new Date());
            tSystemDictionary.setUpdateUserId(userInfo.getId());
            int addSysDict = sysDictMapper.insertSelective(tSystemDictionary);
            if (addSysDict > 0){
                SystemDictionaryDto systemDictionaryDto = new SystemDictionaryDto();
                BeanUtils.copyProperties(tSystemDictionary,systemDictionaryDto);
                return systemDictionaryDto;
            }
        }
        return null;
    }

    /**
     * 修改字典
     * @param systemDictionaryVO
     * @param userInfo
     * @return
     */
    @Override
    public SystemDictionaryDto updateSysDict(SystemDictionaryVO systemDictionaryVO, UserDto userInfo) {
        if (null != systemDictionaryVO && null != systemDictionaryVO.getId()){
            TSystemDictionary tSystemDictionary = new TSystemDictionary();
            BeanUtils.copyProperties(systemDictionaryVO,tSystemDictionary);
            //组id100课程分类 200活动分类
            if (null != systemDictionaryVO.getGroupId() && systemDictionaryVO.getGroupId().equals("100")){
                tSystemDictionary.setGroupName("课程分类");
            }else if (null != systemDictionaryVO.getGroupId() && systemDictionaryVO.getGroupId().equals("200")){
                tSystemDictionary.setGroupName("活动分类");
            }else if (null != systemDictionaryVO.getGroupId() && systemDictionaryVO.getGroupId().equals("500")){
                tSystemDictionary.setGroupName("师资分类");
            }
            tSystemDictionary.setUpdateDate(new Date());
            tSystemDictionary.setUpdateUserId(userInfo.getId());
            int updateSysDict = sysDictMapper.updateByPrimaryKeySelective(tSystemDictionary);
            if (updateSysDict > 0){
                SystemDictionaryDto systemDictionaryDto = new SystemDictionaryDto();
                BeanUtils.copyProperties(tSystemDictionary,systemDictionaryDto);
                return systemDictionaryDto;
            }
        }
        return null;
    }

    /**
     * 逻辑删除字典
     * @param systemDictionaryVO
     * @param userInfo
     * @return
     */
    @Override
    public SystemDictionaryDto deleteSysDict(SystemDictionaryVO systemDictionaryVO, UserDto userInfo) {
        if (null != systemDictionaryVO && null != systemDictionaryVO.getId()){
            TSystemDictionary tSystemDictionary = new TSystemDictionary();
            BeanUtils.copyProperties(systemDictionaryVO,tSystemDictionary);
            tSystemDictionary.setIsDel(1);
            tSystemDictionary.setUpdateDate(new Date());
            tSystemDictionary.setUpdateUserId(userInfo.getId());
            int updateSysDict = sysDictMapper.updateByPrimaryKeySelective(tSystemDictionary);
            if (updateSysDict > 0){
                SystemDictionaryDto systemDictionaryDto = new SystemDictionaryDto();
                BeanUtils.copyProperties(tSystemDictionary,systemDictionaryDto);
                return systemDictionaryDto;
            }
        }
        return null;
    }

    /**
     * 获取字典列表
     * @param systemDictionaryVO
     * @return
     */
    @Override
    public ResultPage<List<SystemDictionaryDto>> selectSysDictList(SystemDictionaryVO systemDictionaryVO) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(systemDictionaryVO.getPageNum());
        pageInfo.setPageSize(systemDictionaryVO.getPageSize());
        Page<List<SystemDictionaryDto>> reslut = pageBuild(pageInfo).doSelectPage(()->{
           List<SystemDictionaryDto> list = sysDictMapper.selectSysDictList(systemDictionaryVO);
           for (SystemDictionaryDto systemDictionaryDto:list) {
               //组id100课程分类 200活动分类，300课程协议，400活动协议, 500师资团队
               if (null != systemDictionaryDto.getGroupId() && systemDictionaryDto.getGroupId().equals("100")){
                   //课程数
                   int courseNum = courseMapper.getCourseNumByDict(systemDictionaryDto.getId(),"course");
                   systemDictionaryDto.setCourseNum(courseNum);
               }else {
                   systemDictionaryDto.setCourseNum(0);
               }

               if (null != systemDictionaryDto.getGroupId() && systemDictionaryDto.getGroupId().equals("200")){
                   //活动数
                   int courseNum = courseMapper.getCourseNumByDict(systemDictionaryDto.getId(),"activity");
                   systemDictionaryDto.setCourseNum(courseNum);
               }else {
                   systemDictionaryDto.setCourseNum(0);
               }

               if (null != systemDictionaryDto.getGroupId() && systemDictionaryDto.getGroupId().equals("500")){
                   //师资数
                   int courseNum = courseMapper.getCourseNumByDict(systemDictionaryDto.getId(),"team");
                   systemDictionaryDto.setCourseNum(courseNum);
               }else {
                   systemDictionaryDto.setCourseNum(0);
               }
           }
        });
        return pageTransform(reslut);
    }

    @Override
    public SystemDictionaryDto selectSysDictInfo(SystemDictionaryVO systemDictionaryVO) {
        if (null != systemDictionaryVO && null != systemDictionaryVO.getId()){
            TSystemDictionary tSystemDictionary = sysDictMapper.selectSysDictInfo(systemDictionaryVO.getId());
            if (null != tSystemDictionary){
                SystemDictionaryDto systemDictionaryDto = new SystemDictionaryDto();
                BeanUtils.copyProperties(tSystemDictionary,systemDictionaryDto);
                return systemDictionaryDto;
            }
        }
        return null;
    }
    
    @Override
    public SystemDictionaryDto selectSysDictInfoByGroupId(SystemDictionaryVO systemDictionaryVO) {
        if (null != systemDictionaryVO && !StringUtils.isEmpty(systemDictionaryVO.getGroupId())){
            List<SystemDictionaryDto> dictionaryDtoList = sysDictMapper.selectSysDictList(systemDictionaryVO);
            if (!CollectionUtils.isEmpty(dictionaryDtoList)){
                return dictionaryDtoList.get(0);
            }
        }
        return null;
    }
    
    /**
     * 获取全部分类
     * @param systemDictionaryVO
     * @return
     */
    @Override
    public List<SystemDictionaryDto> selectAllClassifyList(SystemDictionaryVO systemDictionaryVO) {
        return sysDictMapper.selectAllClassifyList(systemDictionaryVO);
    }


}
